﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using lutianqi.Model;
using lutianqi.Helpers;

namespace lutianqi.DAL
{
    public class WeatherDAL
    {
        public static WeatherInfoConverted GetConvertedWeatherInfo(Weather weather)
        {
            WeatherInfoConverted result = new WeatherInfoConverted();
            result.city = weather.weatherinfo.city;
            result.cityid = weather.weatherinfo.cityid;
            result.city_en = weather.weatherinfo.city_en;
            result.date_y = weather.weatherinfo.date_y;
            result.fchh = weather.weatherinfo.fchh;
            result.week = weather.weatherinfo.week;
            result.IsNight = weather.weatherinfo.fchh == "18";
            ConvertWeatherInfo(weather, ref result);
            return result;
        }

        static void ConvertWeatherInfo(Weather weather, ref WeatherInfoConverted convert)
        {
            if (weather.weatherinfo.fchh == "18")
            {
                convert.img_title11 = string.Empty;
                convert.img_title12 = weather.weatherinfo.img_title1;
                convert.img_title21 = weather.weatherinfo.img_title2;
                convert.img_title22 = weather.weatherinfo.img_title3;
                convert.img_title31 = weather.weatherinfo.img_title4;
                convert.img_title32 = weather.weatherinfo.img_title5;
                convert.img_title41 = weather.weatherinfo.img_title6;
                convert.img_title42 = weather.weatherinfo.img_title7;
                convert.img_title51 = weather.weatherinfo.img_title8;
                convert.img_title52 = weather.weatherinfo.img_title9;
                convert.img_title61 = weather.weatherinfo.img_title10;
                convert.img_title62 = weather.weatherinfo.img_title11;
                convert.img_title71 = weather.weatherinfo.img_title12;

                convert.temp11 = string.Empty;
                convert.temp12 = weather.weatherinfo.temp1.Split(new char[] { '~' })[0];
                convert.temp21 = weather.weatherinfo.temp1.Split(new char[] { '~' })[1];
                convert.temp22 = weather.weatherinfo.temp2.Split(new char[] { '~' })[0];
                convert.temp31 = weather.weatherinfo.temp2.Split(new char[] { '~' })[1];
                convert.temp32 = weather.weatherinfo.temp3.Split(new char[] { '~' })[0];
                convert.temp41 = weather.weatherinfo.temp3.Split(new char[] { '~' })[1];
                convert.temp42 = weather.weatherinfo.temp4.Split(new char[] { '~' })[0];
                convert.temp51 = weather.weatherinfo.temp4.Split(new char[] { '~' })[1];
                convert.temp52 = weather.weatherinfo.temp5.Split(new char[] { '~' })[0];
                convert.temp61 = weather.weatherinfo.temp5.Split(new char[] { '~' })[1];
                convert.temp62 = weather.weatherinfo.temp6.Split(new char[] { '~' })[0];
                convert.temp71 = weather.weatherinfo.temp6.Split(new char[] { '~' })[1];

                convert.wind11 = string.Empty;
                convert.wind12 = DealWithWind(weather.weatherinfo.wind1, weather.weatherinfo.fl1, true);
                convert.wind21 = DealWithWind(weather.weatherinfo.wind1, weather.weatherinfo.fl1, true);
                convert.wind22 = DealWithWind(weather.weatherinfo.wind2, weather.weatherinfo.fl2, true);
                convert.wind31 = DealWithWind(weather.weatherinfo.wind2, weather.weatherinfo.fl2, true);
                convert.wind32 = DealWithWind(weather.weatherinfo.wind3, weather.weatherinfo.fl3, true);
                convert.wind41 = DealWithWind(weather.weatherinfo.wind3, weather.weatherinfo.fl3, true);
                convert.wind42 = DealWithWind(weather.weatherinfo.wind4, weather.weatherinfo.fl4, true);
                convert.wind51 = DealWithWind(weather.weatherinfo.wind4, weather.weatherinfo.fl4, true);
                convert.wind52 = DealWithWind(weather.weatherinfo.wind5, weather.weatherinfo.fl5, true);
                convert.wind61 = DealWithWind(weather.weatherinfo.wind5, weather.weatherinfo.fl5, true);
                convert.wind62 = DealWithWind(weather.weatherinfo.wind6, weather.weatherinfo.fl6, true);
                convert.wind71 = DealWithWind(weather.weatherinfo.wind6, weather.weatherinfo.fl6, true);
            }

            else
            {
                convert.img_title11 = weather.weatherinfo.img_title1;
                convert.img_title12 = weather.weatherinfo.img_title2;
                convert.img_title21 = weather.weatherinfo.img_title3;
                convert.img_title22 = weather.weatherinfo.img_title4;
                convert.img_title31 = weather.weatherinfo.img_title5;
                convert.img_title32 = weather.weatherinfo.img_title6;
                convert.img_title41 = weather.weatherinfo.img_title7;
                convert.img_title42 = weather.weatherinfo.img_title8;
                convert.img_title51 = weather.weatherinfo.img_title9;
                convert.img_title52 = weather.weatherinfo.img_title10;
                convert.img_title61 = weather.weatherinfo.img_title11;
                convert.img_title62 = weather.weatherinfo.img_title12;
                convert.img_title71 = string.Empty;

                convert.temp11 = weather.weatherinfo.temp1.Split(new char[] { '~' })[0];
                convert.temp12 = weather.weatherinfo.temp1.Split(new char[] { '~' })[1];
                convert.temp21 = weather.weatherinfo.temp2.Split(new char[] { '~' })[0];
                convert.temp22 = weather.weatherinfo.temp2.Split(new char[] { '~' })[1];
                convert.temp31 = weather.weatherinfo.temp3.Split(new char[] { '~' })[0];
                convert.temp32 = weather.weatherinfo.temp3.Split(new char[] { '~' })[1];
                convert.temp41 = weather.weatherinfo.temp4.Split(new char[] { '~' })[0];
                convert.temp42 = weather.weatherinfo.temp4.Split(new char[] { '~' })[1];
                convert.temp51 = weather.weatherinfo.temp5.Split(new char[] { '~' })[0];
                convert.temp52 = weather.weatherinfo.temp5.Split(new char[] { '~' })[1];
                convert.temp61 = weather.weatherinfo.temp6.Split(new char[] { '~' })[0];
                convert.temp62 = weather.weatherinfo.temp6.Split(new char[] { '~' })[1];
                convert.temp71 = string.Empty;

                convert.wind11 = DealWithWind(weather.weatherinfo.wind1, weather.weatherinfo.fl1, false);
                convert.wind12 = DealWithWind(weather.weatherinfo.wind1, weather.weatherinfo.fl1, false);
                convert.wind21 = DealWithWind(weather.weatherinfo.wind2, weather.weatherinfo.fl2, false);
                convert.wind22 = DealWithWind(weather.weatherinfo.wind2, weather.weatherinfo.fl2, false);
                convert.wind31 = DealWithWind(weather.weatherinfo.wind3, weather.weatherinfo.fl3, false);
                convert.wind32 = DealWithWind(weather.weatherinfo.wind3, weather.weatherinfo.fl3, false);
                convert.wind41 = DealWithWind(weather.weatherinfo.wind4, weather.weatherinfo.fl4, false);
                convert.wind42 = DealWithWind(weather.weatherinfo.wind4, weather.weatherinfo.fl4, false);
                convert.wind51 = DealWithWind(weather.weatherinfo.wind5, weather.weatherinfo.fl5, false);
                convert.wind52 = DealWithWind(weather.weatherinfo.wind5, weather.weatherinfo.fl5, false);
                convert.wind61 = DealWithWind(weather.weatherinfo.wind6, weather.weatherinfo.fl6, false);
                convert.wind62 = DealWithWind(weather.weatherinfo.wind6, weather.weatherinfo.fl6, false);
                convert.wind71 = string.Empty;
            }
        }

        static string DealWithWind(string direction, string speed, bool first)
        {
            string tp1, tp2;
            Regex r = new Regex("(无持续风向|东风|南风|西风|北风|东南风|西南风|西北风|东北风)");
            MatchCollection m = r.Matches(direction.Replace("微风", "无持续风向"));
            string[] tparrstr = speed.Replace("转", "-").Split(new char[] { '-' });

            if (m.Count == 1)//白天和夜晚是同一个风向
            {
                if (tparrstr.Length == 1)//白天和夜晚是同一个风速
                {
                    tp1 = m[0].Value + tparrstr[0];
                    tp2 = m[0].Value + tparrstr[0];
                }
                else//白天和夜晚不是同一个风速
                {
                    tp1 = m[0].Value + tparrstr[0];
                    tp2 = m[0].Value + tparrstr[1];
                }
            }
            else//白天和夜晚不是同一个风向
            {
                if (tparrstr.Length == 1)//白天和夜晚是同一个风速
                {
                    tp1 = m[0].Value + tparrstr[0];
                    tp2 = m[1].Value + tparrstr[0];
                }
                else//白天和夜晚不是同一个风速
                {
                    tp1 = m[0].Value + tparrstr[0];
                    tp2 = m[1].Value + tparrstr[1];
                }
            }

            //处理部分风速无“级”的问题
            if (!tp1.EndsWith("级"))
                tp1 += "级";
            if (!tp2.EndsWith("级"))
                tp2 += "级";

            if (first)
                return tp1;
            else
                return tp2;
        }

    }
}